#define _CRT_SECURE_NO_WARNINGS 1

#include "ST.h"

void StackInit(Stack* ps)
{
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

void StackPush(Stack* ps, STDataType data)
{
	assert(ps);
	size_t NewCapacity = ps->_capacity == 0 ? 4 * sizeof(STDataType) : 2 * (size_t)ps->_capacity;
	STDataType* tmp = NULL;
	(STDataType*)tmp = (STDataType*)realloc(ps->_a, NewCapacity);
	if (tmp == NULL)
	{
		perror("Realloc fail!");
		return;
	}
	ps->_a = tmp;
	ps->_a[ps->_top++] = data;
	ps->_capacity = (int)NewCapacity;
}

void StackPop(Stack* ps)
{
	assert(ps);
	ps->_top--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	return ps->_a[ps->_top - 1];
}

int StackSize(Stack* ps)
{
	assert(ps);
	return ps->_top;
}

int StackEmpty(Stack* ps)
{
	assert(ps);
	if (ps->_top == 0)
	{
		return -1;
	}
	else if (ps->_top > 0)
	{
		return 0;
	}
	else
	{
		perror("top fail!");
		exit(1);
	}
}

void StackDestroy(Stack* ps)
{
	assert(ps);
	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}